package com.zycfc.zsf.boot.core.autoconfigure;

import org.springframework.context.support.*;
import com.zycfc.zsf.boot.util.time.*;
import com.zycfc.zsf.boot.common.context.*;
import org.springframework.context.annotation.*;
import org.springframework.boot.env.*;
import org.springframework.core.env.*;
import org.springframework.core.io.*;
import java.io.*;
import com.zycfc.zsf.boot.util.text.*;
import java.util.*;
import org.slf4j.*;

@Configuration
public class YmlSourcesLoadConfiguration
{
    private static final Logger log;
    
    @Bean
    public PropertySourcesPlaceholderConfigurer properties(final ConfigurableEnvironment environment) {
        System.setProperty("logging.path", "F:/test" + DateUtils.getCurTM() + "/logs");
        final String active = environment.getProperty("spring.profiles.active");
        final String applicationName = environment.getProperty("spring.application.name");
        final String namespace = environment.getProperty("zsf.all.namespace");
        ZsfEnvConstants.put(ZsfEnvConstants.APPLICATION_NAME_KEY, applicationName);
        ZsfEnvConstants.put(ZsfEnvConstants.PROFILES_ACTIVE_KEY, active);
        ZsfEnvConstants.put(ZsfEnvConstants.ZSF_ALL_NAMESPACE, namespace);
        final MutablePropertySources propertySources = environment.getPropertySources();
        final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        final String pathObj = (String)environment.getProperty("zsf.boot.core.ymlResourcePath", (Class)String.class, (Object)null);
        if (pathObj != null) {
            final String[] split;
            final String[] paths = split = pathObj.split(",");
            for (final String path : split) {
                this.load(propertySources, path);
                final String[] array = path.split("[.]");
                final String envPath = array[0] + "-" + active + "." + array[1];
                this.load(propertySources, envPath);
            }
        }
        final boolean globalEnabled = (boolean)environment.getProperty("zsf.boot.bizdata.global.enabled", (Class)Boolean.class, (Object)false);
        final boolean cacheEnabled = (boolean)environment.getProperty("zsf.boot.bizdata.cache.enabled", (Class)Boolean.class, (Object)false);
        if (globalEnabled || cacheEnabled) {
            final String globalBizdataPath = "config/zsf/bizdata/config/global-bizdata-" + active + ".yml";
            this.load(propertySources, globalBizdataPath);
            YmlSourcesLoadConfiguration.log.info("^_^\u52a0\u8f7d\u5168\u5c40\u4e1a\u52a1\u6570\u636e\u6e90\uff0c" + globalBizdataPath + "^_^");
        }
        configurer.setPropertySources((PropertySources)propertySources);
        return configurer;
    }
    
    private void load(final MutablePropertySources propertySources, final String path) {
        final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
        List<PropertySource<?>> envList = new ArrayList<PropertySource<?>>();
        try {
            YmlSourcesLoadConfiguration.log.info("^_^\u52a0\u8f7d\u81ea\u5b9a\u4e49YML\u6587\u4ef6\uff0c\u5f53\u524d\u6587\u4ef6\u8def\u5f84:" + path + "^_^");
            envList = (List<PropertySource<?>>)loader.load(path, (Resource)new ClassPathResource(path));
        }
        catch (IOException e) {
            YmlSourcesLoadConfiguration.log.error("path:{}\u4e0d\u5b58\u5728\u8bf7\u786e\u8ba4\uff01", (Object)path);
        }
        catch (Throwable ex) {
            YmlSourcesLoadConfiguration.log.error("path:{}\u4e0d\u5b58\u5728\u8bf7\u786e\u8ba4\uff01\uff01", (Object)path);
        }
        if (!StringUtils.isEmpty((List)envList)) {
            propertySources.addLast((PropertySource)envList.get(0));
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)YmlSourcesLoadConfiguration.class);
    }
}
